/*
 * common.h
 *
 *  Created on: Jan 8, 2012
 *      Author: sitnoam
 */

#ifndef COMMON_H_
#define COMMON_H_

#include <stdlib.h>
#include <setjmp.h>
#include <unistd.h>
#include <signal.h>

#include "user_threads.h"
#include "thread_list.h"
#include "user_mutex.h"

typedef struct uthread_s *uthread;

int is_uthread_initialized;

struct uthread_s {
	int magic;
	uthread_id join_by; /* NOAM: this line can be other than 2nd from now on 05/01/2012 */
	uthread_id join_to;
	int* ret_value_joined; /* for father  joining me */
	uthread_id id;
	int prio;
	int state;
	int ret_value;
	jmp_buf current_instruction; //Boris would not stop crying.
	__useconds_t lock_alarm;
	void* stack;
	void* args;
	UTHREAD_START_ROUTINE func;
//	uthread_id join_to;

/* From here: mutex related fields */
	thread_list *locked_mutexes;
	uthread_mutex_t mutex_waiting_on;
	
	int magic2;
};


void lock_alarm(); 		/* can only work after init of uthreads */
void unlock_alarm(); 	/* can only work after init of uthreads */
void deactivate_thread_and_cs(uthread thread);
void activate_thread(uthread thread);
int uthread_cond_init();

extern uthread all_threads[];
extern uthread current;


/* mutex methods */
int uthread_free_mutexes(uthread thread);
int uthread_mutex_init_general();
void uthread_mutext_free_general();

/* end of mutext methods */

#define HW3_SUCCESS			0
#define HW3_NOMEM			(-1)
#define HW3_INVALID_PARAM	(-2)
#define HW3_FAILURE			(-3)

#endif /* COMMON_H_ */
